package com.leoman.customer.service.impl;

import com.leoman.common.core.ErrorType;
import com.leoman.common.core.Result;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.customer.dao.CustomerDao;
import com.leoman.customer.entity.Customer;
import com.leoman.customer.service.CustomerService;
import com.leoman.utils.ClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by Daisy on 2016/12/27.
 */
@Service
public class CustomerServiceImpl extends GenericManagerImpl<Customer,CustomerDao> implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    /**
     * 保存
     * @param customer
     * @return
     */
    @Override
    @Transactional
    public Result saveCustomer(Customer customer){

        if(customer.getId() == null){
            Customer cu = customerDao.findByDemandNo(customer.getDemandNo());
            if(cu != null){
                return Result.failure(ErrorType.ERROR_CODE_0008);//该需求号已存在
            }
            customer.setStatus(0);
        }else{
            Customer cu = customerDao.findByDemandNoAndId(customer.getDemandNo(), customer.getId());
            if(cu != null){
                return Result.failure(ErrorType.ERROR_CODE_0008);//该需求号已存在
            }
            Customer org = customerDao.findOne(customer.getId());
            ClassUtil.copyProperties(org, customer);
            customer = org;
        }

        customerDao.save(customer);
        return Result.success();
    }

    @Override
    public List<Customer> findByAdminId(Long adminId) {
        return customerDao.findByAdminId(adminId);
    }

    @Override
    public Page<Customer> findPage(Long adminId, String searchContent, String grade, String createDate, Integer start, Integer length){
        StringBuffer sql = new StringBuffer("SELECT \n" +
                "  c.`id`,\n" +
                "  c.`demand_no` AS demandNo,\n" +
                "  c.`name`,\n" +
                "  c.`mobile`,\n" +
                "  FROM_UNIXTIME(cd.`create_date`/1000,'%Y-%m-%d %H:%i') AS createDate,\n" +
                "  FROM_UNIXTIME(cd.`next_time`/1000,'%Y-%m-%d') AS nextTime,\n" +
                "  IF(cd.`grade` = 'FAIL', '战败', cd.`grade`) AS grade,\n" +
                "  a.`name` as adminName \n" +
                "FROM\n" +
                "  t_customer c \n" +
                "  LEFT JOIN t_admin a \n" +
                "    ON a.`id` = c.`admin_id` \n" +
                "  LEFT JOIN t_customer_demand cd \n" +
                "    ON cd.`customer_id` = c.`id` \n" +
                "    AND cd.`id` = \n" +
                "    (SELECT \n" +
                "      MAX(tcd.`id`) AS maxid \n" +
                "    FROM\n" +
                "      t_customer_demand tcd \n" +
                "    WHERE tcd.`customer_id` = c.`id` \n" +
                "    GROUP BY tcd.`customer_id`) \n" +
                "WHERE c.`status` = 0 ");
        if(adminId != null){
            sql.append(" AND a.`id` = "+adminId);
        }
        if(!StringUtils.isEmpty(searchContent)){
            sql.append(" AND (c.`demand_no` LIKE '%"+searchContent+"%' OR c.`name` LIKE '%"+searchContent+"%' OR c.`mobile` LIKE '%"+searchContent+"%' OR a.`name` LIKE '%"+searchContent+"%') ");
        }
        if(!StringUtils.isEmpty(grade)){
            sql.append(" AND cd.`grade` = '"+grade+"' ");
        }
        if(!StringUtils.isEmpty(createDate)){
            sql.append(" AND DATE(FROM_UNIXTIME(cd.`create_date`/1000,'%Y-%m-%d')) = DATE(STR_TO_DATE('"+createDate+"', '%Y-%m-%d')) ");
        }
        sql.append(" order by c.id desc ");
        Page page = queryPageBySql(sql.toString(), start, length);
        return page;
    }

}
